1.Use REST streaming to connect products directly to nest services from cloud to cloud integrations.
2.In REST streaming, instead of returning information and close the connection, the NEST api keeps the connection open, and the REST api uses the open connection, and users can receive new state.
3.REST streaming allows for only one token(one user) per socket.
4.What is SSE(server-sent-events): a web app subscribes to a stream of updates generated by server, and whenever a new event occurs, it will send to the client.
AJAX:
- Polling: the application repeatedly polls server for data.
- Long Polling: the server does not have the data available, the server holds the data until new data is made available(Hanging GET).
5.server-sent events are sent over http, but they do not require a special protocol or server implementation get working. When use server-sent requests, a server can push data to client whenever it wants, without making initial request, update can be streamed to clients as they happen. SSEs often use single un-directional channel between client and server.
Diff between SSE and Longpolling is: SSE is handled by browser and users have to listen for messages.
Server Sent Events vs Websockets:
- websocket provides richer protocol to perform bi-directional, full-duplex communication, two way channel for games, messaging apps, and for cases when need real-time communications for both directions.
- Server Sent Events: sometimes not needed to perform data sent from the client. Only need is to update the event from the server side.
//JavaScript API:
if(!!window.eventSource) {
var source = new EventSource('stream.php');
} else {
//result to xhr polling;
}
//set up a handler for the message event
source.addEventListener('message', function(e) {
console.log(e.data);
}, false);
source.addEventListener('open', function(e) {
//connection is opened
}, false);
source.addEventListener('error', function(e) {
if(e.readyState == EventSource.CLOSED) {
//connection is closed;
}
}, false);
Server implementation in NodeJS:
var http = require('http');
var sys = require('sys');
var fs = require('fs');
http.createServer(function(req, res) {
if(req.headers.accept && req.headers.accept == 'text/event-stream') {
if(req.url == '/events') {
sendSSE(req, res);
} else {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(fs.readFileSync(__dirname + '/sse-node/html'));
res.end();
}
}).listen(8000);
function.sendSSE(req, res) {
res.writeHead(200, {
'Content-Type':'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
});
var id = (new Date()).toLocaleTimeString();
//send a SSE every 5 seconds on a single connection
setInterval(function() {
constrcutSSE(res, id, (new Date()).toLocaleTimeString());
}
function constructSSE(res, id, data) {
res.write('id: ' + id + '\n');
res.write("data: " + data + '\n\n');
}
function debugHeaders(req) {
sys.put('URL: ' + req.url);
for(var key in req.headers) {
sys.put(key + ': ' + req.headers[key]);
}
sys.put('\n\n');
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。